//
//  ServerView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/3/9.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol ServerViewDelegate {
    // 服务器地址
    func server_url(url:String?)
    // 下拉箭头点击
    func server_click_down_arrow()
}

class ServerView: UIView {
    
    // 输入
    private let input = TextFieldOffsetPlaceHolder()
    // 代理
    public var delegate:ServerViewDelegate?
    
    public var resetInputUrl:String? {
        didSet {
            input.text = resetInputUrl
        }
    }
    
    override func layoutSubviews() {
        let back = UIView(frame: CGRect(x: 0, y: 0, width: frame.width, height: MARGIN70))
        back.backgroundColor = UIColor.white
        // 标题
        let label = UILabel(title: "服务器", color: COLOR_NICE, size: 17)
        label.frame = CGRect(x: MARGIN, y: 0, width: MARGIN * 4.8, height: MARGIN70)
        back.addSubview(label)
        // 输入
        input.frame = CGRect(x: MARGIN, y: 0, width: frame.width - MARGIN25, height: MARGIN70)
        input.keyboardType = .asciiCapable
        input.textColor = UIColor.lightGray
        input.font = UIFont.systemFont(ofSize: 13)
        input.setValue(MARGIN * 5, forKey: "paddingLeft")
        back.addSubview(input)
        addSubview(back)
        // 回显数据
        var ip = UserDefaults.standard.string(forKey: SP_IP)
        if ip == nil {
            ip = SP_INNTER
        }
        input.text = ip
        // 下拉箭头
        let img = UIButton()
        let imgX = frame.width - MARGIN40
        img.setImage(UIImage(named: "IMG_ArrowDown_11x7_"), for: .normal)
        img.frame = CGRect(x: imgX, y: 0, width: MARGIN40, height: MARGIN70)
        addSubview(img)
        img.addTarget(self, action: #selector(clickDownArrow), for: .touchUpInside)
        // 确认修改按钮
        let saveBtn = UIButton(title: "确认修改", name: "button_orange",radius: 5)
        let saveBtnY = input.frame.maxY + MARGIN35
        saveBtn.frame = CGRect(x: MARGIN, y: saveBtnY, width: frame.width - MARGIN * 2, height: MARGIN50)
        addSubview(saveBtn)
        // 点击事件
        saveBtn.addTarget(self, action: #selector(clickServerSave), for: .touchUpInside)
    }
 
    @objc func clickServerSave() -> Void {
        delegate?.server_url(url: input.text)
    }
    
    @objc func clickDownArrow() -> Void {
        delegate?.server_click_down_arrow()
    }
}
